package coolq.yaozhenyong.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.forte.qqrobot.beans.messages.result.GroupInfo;

import coolq.yaozhenyong.Cqrobotjar2Application;
import coolq.yaozhenyong.custom.BackEndHttpRequest;
import coolq.yaozhenyong.custom.GroupHtmlAndQqService;
import coolq.yaozhenyong.custom.HtmlAndQq;
import coolq.yaozhenyong.custom.UpdateGroupList;
import coolq.yaozhenyong.thread.QqRobot;


@Controller
public class HomeController {
	
	
	
	@RequestMapping(value ="/open")
	@ResponseBody
	private Object open(HttpServletResponse response) {
		/*GoTulin tulin = new GoTulin();
		tulin.setPerception(new Perception(new InputText("你好呀"),new InputImage(""),new SelfInfo(new Location("湖北","武汉",""))));
		tulin.setUserInfo(new UserInfo("929c4195a57b40d183fc498ebfe3ef1a",""));
		return tulin;*/
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String startDate = df.format(new Date());
			/*** 
			 * 给线程指定超时时间，超时则停止线程
			 * 搬运地址 https://www.cnblogs.com/janetshen/p/7976801.html
			 */
			QqRobot task1 = new QqRobot("one", 4);
			Thread t1 = new Thread(task1);
			t1.start();
			try { 
				t1.join(10000); // 在主线程中等待t1执行5秒
			}catch (InterruptedException e) {
				e.printStackTrace();
				return true;
			}
			String endDate = df.format(new Date());
			long starttime = df.parse(startDate).getTime();
			long endtime = df.parse(endDate).getTime();
			int minutes = (int) ((endtime-starttime)/(1000));
			if(minutes<10) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
	/****
	 * 关闭
	 * @param response
	 * @return
	 */
	@RequestMapping(value ="/close")
	@ResponseBody
	private Object close(HttpServletResponse response) {
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		System.exit(0);
		Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");//关闭正在运行的cmd窗口
        } catch (Exception e) {
            e.printStackTrace();
        }
		return true;
	}
	@RequestMapping(value="/tosave")
	@ResponseBody
	private Object toxiaoxi(HttpServletResponse response) {
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		System.out.println(" 已运行tosave方法，将会获取机器人加入的QQ群 ");
		UpdateGroupList.uodate(Cqrobotjar2Application.msgSender, MyqqRobot.cqCodeUtil);
		return true;
	}
	/***
	 * 对外村庄的接口
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/coc")
	@ResponseBody
	private Object coc(String tag) {
		String url = "https://api.clashofclans.com/v1/players/%23" + tag;
		String sendGetCoc = BackEndHttpRequest.sendGetCoc(url);
		return sendGetCoc;
	}
	/***
	 * 获取联赛对战列表
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/towars")
	@ResponseBody
	private Object towars(String tag) {
		String url = BackEndHttpRequest.sendGetCoc("https://api.clashofclans.com/v1/clanwarleagues/wars/%23"+tag);
		JSONObject jsonx = JSON.parseObject(url);
		return jsonx;
	}
	/***
	 * 获取联赛对战列表
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/league")
	@ResponseBody
	private Object league(String tag) {
		String url = "https://api.clashofclans.com/v1/clans/%23" + tag+"/currentwar/leaguegroup";
		String sendGetCoc = BackEndHttpRequest.sendGetCoc(url);
		return sendGetCoc;
	}
	/***
	 * 获取联赛对战列表
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/warlog")
	@ResponseBody
	private Object warlog(String tag) {
		String sendGetCoc = BackEndHttpRequest.sendGetCoc("https://api.clashofclans.com/v1/clans/%23"+tag+"/warlog");
		try {
			JSONObject jsonx = JSON.parseObject(sendGetCoc); 
			return jsonx;
			
		} catch (Exception e) {
			return sendGetCoc;
		}
	}
	/***
	 * 对外部落的接口
	 * @param tag
	 * @return
	 */
	@RequestMapping(value="/coctest")
	@ResponseBody
	public String coctest(String tag) {
		String url = "https://api.clashofclans.com/v1/clans/%23"+tag;
		String sendGetCoc = BackEndHttpRequest.sendGetCoc(url);
		return sendGetCoc;
	}
	/***
	 * QQ发送
	 * @param qq
	 * @param msg
	 * @return
	 */
	@RequestMapping(value="/sendPrivateMsg")
	@ResponseBody
	public Object sendPrivateMsg(String qq,String msg) {
		try {
			Cqrobotjar2Application.msgSender.SENDER.sendPrivateMsg(qq, msg);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	/***
	 * QQ群发送
	 * @param qq
	 * @param msg
	 * @return
	 */
	@RequestMapping(value="/sendGroupMsg")
	@ResponseBody
	public Object sendGroupMsg(String group,String msg) {
		try {
			Cqrobotjar2Application.msgSender.SENDER.sendGroupMsg(group, msg);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	/***
	 * QQ群发送
	 * 推送专用
	 * @param qq
	 * @param msg
	 * @return
	 */
	@RequestMapping(value="/groupMsg")
	@ResponseBody
	public Object groupMsg(String group,String msg) {
		try {
			msg = HtmlAndQq.broadcastGroup(msg, group, Cqrobotjar2Application.msgSender);
			boolean sendGroupMsg = Cqrobotjar2Application.msgSender.SENDER.sendGroupMsg(group, msg);
			return sendGroupMsg;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/***
	 *  刷新群信息
	 * @param group
	 * @return
	 */
	@RequestMapping(value="/updateGroup")
	@ResponseBody
	public Object updateGroup(String group) {
		GroupInfo groupInfo = Cqrobotjar2Application.msgSender.GETTER.getGroupInfo(group);
		return groupInfo.getOriginalData();
	}
	/***
	 * 退出群聊
	 * @param group
	 * @return
	 */
	@RequestMapping(value="/GroupLeave")
	@ResponseBody
	public Boolean GroupLeave(String group) {
		try {
			Cqrobotjar2Application.msgSender.SETTER.setGroupLeave(group);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	/***
	 * 获取群信息
	 * @param group
	 * @return
	 */
	@RequestMapping(value="/GroupMemberList")
	@ResponseBody
	public Object GroupMemberList(String group) {
		try {
			com.forte.qqrobot.beans.messages.result.GroupMemberList groupMemberList = Cqrobotjar2Application.msgSender.GETTER.getGroupMemberList(group);
			return groupMemberList.getOriginalData();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/***
	 * 踢出群成员
	 * @param group
	 * @return
	 */
	@RequestMapping(value="/setGroupMemberKick")
	@ResponseBody
	public Boolean setGroupMemberKick(String group,String qq,boolean type) {
		try {
			Cqrobotjar2Application.msgSender.SETTER.setGroupMemberKick(group, qq,type);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
}
